package com.zjzaki.thingsboard.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zjzaki.thingsboard.entity.Device;

import java.util.ArrayList;
import java.util.List;

/**
 * @date 2022年10月07日 21:35:18
 */
public class Json2ListDevice {

    /**
     * 将获取到的设备信息JSON数据转换成实体对象Device
     * @param jsonStr
     * @return
     */
    public static List<Device> getDeviceList(String jsonStr){
        List<Device> list = new ArrayList<>();
        JSONObject deviceJson = JSONObject.parseObject(jsonStr);
        JSONArray jsonArray = deviceJson.getJSONArray("data");

        if(jsonArray != null){
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject tempJson = (JSONObject) jsonArray.get(i);
                Device device = new Device();
                device.setId_entityType(tempJson.getJSONObject("id").getString("entityType"));
                device.setId_id(tempJson.getJSONObject("id").getString("id"));
                device.setCreatedTime(tempJson.getLong("createdTime"));
                // device.setAdditionalInfo_geteway(tempJson.getJSONObject("additionalInfo").getBoolean("gateway"));
                //  device.setAdditionalInfo_overwriteActivityTime(tempJson.getJSONObject("additionalInfo").getBoolean("overwriteActivityTime"));
                // device.setAdditionalInfo_description(tempJson.getJSONObject("additionalInfo").getString("description"));
                device.setTenantId_entityType(tempJson.getJSONObject("tenantId").getString("entityType"));
                device.setTenantId_id(tempJson.getJSONObject("tenantId").getString("id"));
                device.setCustomerId_entityType(tempJson.getJSONObject("customerId").getString("entityType"));
                device.setCustomerId_id(tempJson.getJSONObject("customerId").getString("id"));
                device.setName(tempJson.getString("name"));
                device.setType(tempJson.getString("type"));
                device.setLabel(tempJson.getString("label"));
                device.setDeviceProfileId_entityType(tempJson.getJSONObject("deviceProfileId").getString("entityType"));
                device.setDeviceProfileId_id(tempJson.getJSONObject("deviceProfileId").getString("id"));
                device.setDeviceData_configuration_type(tempJson.getJSONObject("deviceData").getJSONObject("configuration").getString("type"));
                device.setDeviceData_transportConfiguration_type(tempJson.getJSONObject("deviceData").getJSONObject("transportConfiguration").getString("type"));
                device.setFirmwareId(tempJson.getString("firmwareId"));
                device.setSoftwareId(tempJson.getString("softwareId"));
                device.setExternalId(tempJson.getString("externalId"));
                device.setCustomerTitle(tempJson.getString("customerTitle"));
                device.setDeviceProfileName(tempJson.getString("deviceProfileName"));

                list.add(device);
            }
        }
        return list;
    }
}
